package com.skywaregroup.arenal;

import java.util.*;

import javax.persistence.*;
import javax.xml.bind.annotation.*;

/*@Column(
	    name="columnName";
	    boolean unique() default false;
	    boolean nullable() default true;
	    boolean insertable() default true;
	    boolean updatable() default true;
	    String columnDefinition() default "";
	    String table() default "";
	    int length() default 255;
	    int precision() default 0; // decimal precision
	    int scale() default 0; // decimal scale */
//Mikhail Kalatchev, SKYWARE Group, 2012-2014

/**
 * Represents region as Bulgaria's administrative purposes.
 * @author Mikhail Kalatchev
 */
@XmlRootElement(name="Region")
@XmlAccessorType(XmlAccessType.NONE) 
@Entity 
@Table(name="regions") 
public class Region {
	
	/**
	 * Administrative identifier. Primary key.
	 * Used by Ministry of Health for practice codes (first two letters of the registration code).
	 * Matches exactly the health region codes.
	 */
	@Id
	@Column(name="adm_code", nullable=false)
	public Integer admCode;
	
	/**
	 * Identifier of a BMA body.
	 * BMA stands for Bulgarian Medical Association (BLS).
	 * Used for identification of primary physician registration (first two letters of the UIN).
	 */
	@Column(name="bma_body", nullable=false)
	public Integer bmaCode;
	
	/**
	 * Name of region.
	 */
	@XmlElement(name="Name") 
	@Column(name="name", nullable=false, length=50) 
	public String name;

	@XmlElement(name="Code")
	public String getCode() {
		return String.format("%02d", this.admCode);
	}
	
	public void setCode(String s){
		if (!s.matches("^\\d{2}$")) {throw new IllegalArgumentException("Code must be 2-digits string.");}
		this.admCode = Integer.parseInt(s);
	}
	
	@XmlElement(name="BMACode")
	public String getBMACode() {
		return String.format("%02d", this.bmaCode);
	}
	
	public void setBMACode(String s){
		if (!s.matches("^\\d{2}$")) {throw new IllegalArgumentException("BMACode must be 2-digits string.");}
		this.bmaCode = Integer.parseInt(s);
	}
	
	/**
	 * Municipalities in the region
	 */
	@OneToMany(targetEntity=Municipality.class, fetch=FetchType.LAZY, 
			cascade={CascadeType.PERSIST, CascadeType.REMOVE}, mappedBy="region")
	@XmlElement(name="Municipality")
	public Set<Municipality> municipalities;
 
}
